package com.itown.expo.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.web.servlet.ModelAndView;

import webframe.common.helper.ControllerHelper;

import com.itown.expo.controller.base.MyBaseCommandController;
import com.itown.expo.helper.MyControllerHelper;
import com.itown.expo.model.MainGoodsDecl;

public class MainDeclClearSearchController extends MyBaseCommandController {
	private static Order[] orders = new Order[] { Order.desc("entClearApply"),
			Order.desc("declDate") };

	@Override
	protected ModelAndView handleInner(HttpServletRequest request,
			HttpServletResponse response, Object command)
			throws RuntimeException {
		MainGoodsDecl cdecl = (MainGoodsDecl) command;
		DetachedCriteria criteria = DetachedCriteria
				.forClass(MainGoodsDecl.class);

		ControllerHelper.addEqualRestrictions(criteria, "id", cdecl.getId());
		ControllerHelper.addLikeRestrictions(criteria, "declNo", cdecl
				.getDeclNo());
		ControllerHelper.addLikeRestrictions(criteria, "entName", cdecl
				.getEntName());
		ControllerHelper.addEqualRestrictions(criteria, "backupCode", cdecl
				.getBackupCode());

		if (cdecl.getStatus() == null || cdecl.getStatus().intValue() == 0) {
			cdecl.setStatus(1L);
		}
		if (cdecl.getStatus().intValue() == 1) {
			// 所有需核销
			criteria.add(Restrictions.ge("cellation", 1L));
		} else if (cdecl.getStatus().intValue() == 2) {
			// 新申请
			criteria.add(Restrictions.ge("entClearApply", 1L));
		} else if (cdecl.getStatus().intValue() == 3) {
			// 完成核销
			ControllerHelper.addEqualRestrictions(criteria, "cellation", 2L);
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("appPrefix", getAppPrefix());
		map.put("page", MyControllerHelper.judgeOverrun(ControllerHelper
				.queryPagination(request, orders, criteria)));
		map.put("cdecl", cdecl);
		return new ModelAndView("MainDeclClearList.jsp", "map", map);
	}
}
